/*
 * EditScenLinkTypesAction.java
 *
 * Created on November 15, 2008, 9:27 PM
 *
 * Copyright 2008 David D. Emory
 *
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 *
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster.actions;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.fpdev.core.basenet.BLink;
import org.fpdev.core.basenet.BLinkStreet;
import org.fpdev.core.data.SpatialDataConnection.LinkAttr;
import org.fpdev.apps.rtemaster.RouteMaster;

/**
 *
 * @author demory
 */
public class EditScenLinkTypesAction implements ACAction {

  private Set<BLinkStreet> links_;
  private Map<Short, Integer> overrideTable_;

  public EditScenLinkTypesAction(Set<BLinkStreet> links, Map<Short, Integer> overrideTable) {
    links_ = links;
    overrideTable_ = overrideTable;

    // back up initial conditions
  }

  public String getName() {
    return "Edit Scenario-specific Link Types";
  }

  public boolean doAction(RouteMaster av) {
    clearCurrent(av);
    for (BLinkStreet link : links_) {
      for (Iterator<Map.Entry<Short, Integer>> overrides = overrideTable_.entrySet().iterator(); overrides.hasNext();) {
        Map.Entry<Short, Integer> entry = overrides.next();
        av.getEngine().getBaseNet().setScenLinkOverride(link, entry.getKey(), entry.getValue());
      }
    }
    if(!links_.isEmpty()) {

      // won't compile without this
      Set<BLink> links = new HashSet<BLink>();
      links.addAll(links_);

      av.getEngine().getDataPackage().getSpatialDataConn().modifyLinks(links, new LinkAttr[] { LinkAttr.TORARR} );
      //av.getEngine().getDB().initScenLinkTypes(links_.iterator());
    }
    return true;
  }

  public boolean undoAction(RouteMaster av) {
    return false;
  }

  private void clearCurrent(RouteMaster av) {
    //av.getEngine().getDB().deleteScenLinkTypes(links_.iterator());
    for (Iterator<BLinkStreet> links = links_.iterator(); links.hasNext();)
      av.getEngine().getBaseNet().clearScenLinkOverrides(links.next());

  }

}
